package com.hutianao.gqgl.user.control;

import com.hutianao.gqgl.mybatis.entity.PtRoleEntity;
import com.hutianao.gqgl.mybatis.entity.PtUserEntity;
import com.hutianao.gqgl.mybatis.ztree.ZtreeEntity;
import com.hutianao.gqgl.role.service.RoleService;
import com.hutianao.gqgl.spring.control.BaseController;
import com.hutianao.gqgl.spring.util.DataUitl;
import com.hutianao.gqgl.user.entity.SearchUserEntity;
import com.hutianao.gqgl.user.service.UserService;
import com.hutianao.gqgl.mybatis.entity.PtUserEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hutianao.gqgl.mybatis.flexigrid.PaddingEntity;
import com.hutianao.gqgl.mybatis.flexigrid.PadingRstType;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private UserService userService;
    private PtUserEntity ptUserEntity;

    @Secured("ROLE_gqgy_data_user")
    @RequestMapping("/userPage")
    public String userPage(){
        return "user/userPage";
    }


    @RequestMapping("/queryUserInfoListByName")
    @ResponseBody
    public List<PtUserEntity> queryUserInfoListByName(SearchUserEntity search){
        logger.info(search);
        List<PtUserEntity> list = userService.queryUserInfoListByName(search);
        return list;
    }
    @RequestMapping("/deleteUserInfoByIds")
    @ResponseBody
    public String deleteUserInfoByIds(@RequestParam(value = "ids",required = false) String ids){
        logger.info("ids:" + ids);

        try {
            userService.deleteUserInfoByIds(ids);
        } catch (UncategorizedSQLException e) {
            e.printStackTrace();
            return jsonFail("user.del.user.fail");

        }
        return jsonSuccess("user.del.user.success");
    }

    @RequestMapping("/modifyUserInfoById")
    @ResponseBody
    public String modifyUserInfoById(PtUserEntity ptUserEntity){
        logger.info(ptUserEntity);
        userService.modifyUserInfoById(ptUserEntity);
        return jsonSuccess("user.person.info.success");
    }

    //    @Secured("ROLE_gqgy_data_user_query")
    @RequestMapping("/queryUserInfoListByPage")
    @ResponseBody
    public PadingRstType<PtUserEntity> queryUserInfoListByPage(SearchUserEntity entity, PaddingEntity paddingEntity){
        logger.info(entity);
        logger.info(paddingEntity);
        PadingRstType<PtUserEntity>  padingRstType= userService.queryUserInfoListByPage(entity,paddingEntity);
        return padingRstType;
    }
    @Secured("ROLE_gqgy_data_user_allow")
    @RequestMapping("/allocateRoleTree")
    @ResponseBody
    public List<ZtreeEntity> allocateRoleTree(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid:" + userUuid);

        List<ZtreeEntity> list  = userService.queryRoleTreeList(userUuid);
        return list;
    }
    @Secured("ROLE_gqgy_data_user_allow")
    @RequestMapping("/saveUserIdAndRoleId")
    @ResponseBody
    public String saveUserIdAndRoleId(@RequestParam("userUuid") String userUuid,@RequestParam("roleArray")String roleArray){
        logger.info("userUuid:" + userUuid + "  roleArray" + roleArray);
        userService.saveUserIdAndRoleId(userUuid,roleArray);
        return jsonSuccess("user.role.allocate.success");
    }

    //@Secured("ROLE_gqgy_data_user_allow")
    @RequestMapping("/addUserInfo")
    @ResponseBody
    public String addUserInfo(PtUserEntity ptUserEntity){
        logger.info(ptUserEntity);
        userService.addUserInfo(ptUserEntity);
        return jsonSuccess("user.add.success");
    }

    @RequestMapping("/modifyUserInfo")
    @ResponseBody
    public String modifyUserInfo(PtUserEntity ptUserEntity){
        logger.info(ptUserEntity);
        userService.modifyUserInfo(ptUserEntity);
        return jsonSuccess("user.modify.success");
    }

    @Secured("ROLE_gqgy_data_user_delete")
    @RequestMapping("/deleteUserById")
    @ResponseBody
    public String deleteUserById(@RequestParam("userUuidArray") String userUuidArray){
        logger.info("userUuidArray:" + userUuidArray);
        userService.deleteUserById(userUuidArray);
        return jsonSuccess("user.delete.success");
    }

    @Secured("ROLE_gqgy_data_user_delete")
    @RequestMapping("/unlockUserById")
    @ResponseBody
    public String unlockUserById(@RequestParam("userUuidArray") String userUuidArray){
        logger.info("userUuidArray:" + userUuidArray);
        userService.unlockUserById(userUuidArray);
        return jsonSuccess("user.unlock.success");
    }

    @RequestMapping("/queryDepTree")
    @ResponseBody
    public List<ZtreeEntity> queryDepTree(@RequestParam("userUuid") String userUuid){
        logger.info("userUuid:" + userUuid);
        List<ZtreeEntity>  list= userService.queryDepTree(userUuid);
        return list;
    }

    @Secured("ROLE_gqgy_user_dep_allow")
    @RequestMapping("/saveUserDep")
    @ResponseBody
    public String saveUserDep(@RequestParam("userUuid") String userUuid,@RequestParam("depIds") String depIds){
        logger.info("userUuid "+userUuid + " depIds "+depIds);
        userService.saveUserDep(userUuid,depIds);
        return jsonSuccess("user.dep.allocate.success");
    }
}






